package com.ce.pms.base.dao.impl;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.ce.pms.base.dao.IPmsBaseHibernateDao;
import com.sitechasia.webx.core.dao.IDomainObjectQueryCache;
import com.sitechasia.webx.core.dao.hibernate3.BaseHibernateDao;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述:该类主要是重新包装BaseHibernateDao对象,并屏蔽他的getHibernateTemple()与getSession()方法.
 * 数据拦截针对的就是这个类中的方法进行拦截
 * PmsBaseDaoImpl中调用的方法主要是这个类中的
 * @author creator 燕大为
 *<p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
public class PmsBaseHibernateDao extends BaseHibernateDao implements IPmsBaseHibernateDao{

	public void clear() {
		getSession().clear();

	}

	/**
	 * 功能描述:加载指定标识的对象<br>
	 * @param entiyClass
	 * @param id
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public Object loadById(Class entiyClass,Serializable id){
		return getSession().load(entiyClass, id);
	}

	public Criteria createCriteriaBy(Class entityClass, String name, Object value) {
		Criteria criteria = getSession().createCriteria(entityClass);
		criteria.add(Restrictions.eq(name, value));
		return criteria;
	}



	public Criteria createCriteriaByLike(Class entityClass, String name, String value) {
		Criteria criteria = getSession().createCriteria(entityClass);
		criteria.add(Expression.like(name, value,MatchMode.ANYWHERE));
		return criteria;
	}


	public Criteria createCriteriaUniqueBy(Class entityClass,String name, Object value) {
		Criteria criteria = getSession().createCriteria(entityClass);
		criteria.add(Restrictions.eq(name, value));
		return criteria;
	}


	public Query createQuery(String hql) {
		Query query = getSession().createQuery(hql);
		return query;
	}


	public SQLQuery createSQLQuery(String sql)
	{
		SQLQuery query  = getSession().createSQLQuery(sql);
		return query;
	}


	public void executeSQL(final String sql, final Object[] args) {
		 HibernateCallback call = new HibernateCallback() {

				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					SQLQuery query = session.createSQLQuery(sql);

					if (args != null) {
						for (int i = 0; i < args.length; i++) {
							query.setParameter(i,args[i]);
						}
					}

					query.executeUpdate();

					return null;
				}

	        };
			this.getHibernateTemplate().execute(call);

	}


	public void executeSQL(final String sql, final Object[][] args) {
		getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException,

SQLException {
				Connection conn = session.connection();
				PreparedStatement st =  session.connection().prepareStatement(sql);
				conn.setAutoCommit(false);

				if (args != null) {

					for(Object o[]:args) {
						for (int i = 0 ; i < o.length; i++ ) {
							st.setObject(i + 1 , o[i]);
						}
						st.executeUpdate();
					}

				} else {
					st.executeUpdate();
				}

				conn.commit();
				return null;
			}
		});

	}


	public void executeSQLBatch(final String sql,final List<Object[]> parameters){
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				Connection conn = session.connection();
				PreparedStatement pt =  conn.prepareStatement(sql);
				for(Object[] obj : parameters){
					for(int i=0;i<obj.length;i++){
						pt.setObject(i+1, obj[i]);
					}
					pt.addBatch();
				}
				int[] dig =  pt.executeBatch();
				logger.debug("SQL:"+sql);
				return null;
			}
		});
	}


	public List findInBase(String hql, Object... values) {
		return super.find(hql, values);
	}

	public void flush() {

		getSession().flush();
	}


	@Override
	public List genericFindBy(Class entityClass, String name, Object value) {

		return super.genericFindBy(entityClass, name, value);
	}


	@Override
	public List genericFindByLike(Class entityClass, String name, String value) {

		return super.genericFindByLike(entityClass, name, value);
	}


	@Override
	public Object genericFindUniqueBy(Class entityClass, String name, Object value) {

		return super.genericFindUniqueBy(entityClass, name, value);
	}


	@Override
	public Object genericGet(Class entityClass, Serializable id) {

		return super.genericGet(entityClass, id);
	}


	@Override
	public List genericGetAll(Class entityClass) {

		return super.genericGetAll(entityClass);
	}


	@Override
	public void genericRemove(Class clazz, Object o) {

		super.genericRemove(clazz, o);
	}

	@Override
	public void genericRemoveById(Class entityClass, Serializable id) {

		super.genericRemoveById(entityClass, id);
	}

	@Override
	public  void genericSave(Class clazz, Object o)
	{
		super.genericSave(clazz, o);
	}

	@Override
	public void genericSaveAll(Class clazz, Collection list) {

		super.genericSaveAll(clazz, list);
	}

	@Override
	public Criteria getEntityCriteria(Class entityClass) {

		return super.getEntityCriteria(entityClass);
	}

	@Override
	public boolean isGenericNotUnique(Class entityClass, Object entity, String names) {

		return super.isGenericNotUnique(entityClass, entity, names);
	}

	@Override
	public Page pagedQuery(Criteria criteria, int pageNo, int pageSize) {

		return super.pagedQuery(criteria, pageNo, pageSize);
	}

	@Override
	public Page pagedQuery(String hql, int pageNo, int pageSize, Object... args) {

		return super.pagedQuery(hql, pageNo, pageSize, args);
	}


	public List queryObjectInBase(Criteria criteria) {

		return criteria.list();
	}

	public Object queryUnique(Criteria criteria) {

		return criteria.uniqueResult();
	}

	@Override
	public void setEntityQueryCache(IDomainObjectQueryCache entityQueryCache) {
		super.setEntityQueryCache(entityQueryCache);
	}

}
